﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace Wolter.Stereoscopy.VideoSample
{
    public partial class MainPage : UserControl
    {
        private CaptureSource leftSource;
        private CaptureSource rightSource;
        private VideoBrush leftVideoBrush;
        private VideoBrush rightVideoBrush;

        public MainPage()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(mainPage_Loaded);
        }

        private void mainPage_Loaded(object sender, RoutedEventArgs e)
        {
            ReadOnlyCollection<VideoCaptureDevice> devices = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();

            // set left device collection
            leftDeviceComboBox.ItemsSource = devices;
            // create left capture source (capturing the video)
            leftSource = new CaptureSource();
            // create left video brush (displaying the video)...
            leftVideoBrush = new VideoBrush();
            leftVideoBrush.Stretch = Stretch.Uniform;
            // ...and use this brush to fill the corresponding ui-control
            leftImage.Fill = leftVideoBrush;

            rightDeviceComboBox.ItemsSource = devices;
            rightSource = new CaptureSource();
            rightVideoBrush = new VideoBrush();
            rightVideoBrush.Stretch = Stretch.Uniform;
            rightImage.Fill = rightVideoBrush;
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            if (leftDeviceComboBox.SelectedIndex > -1 && rightDeviceComboBox.SelectedIndex > -1)
            {
                startButton.IsEnabled = false; // for testing purpose only one start possble so far
                startCapturing();
            }
            else
            {
                MessageBox.Show("Please choose two input devices first!", "Alert", MessageBoxButton.OK);
            }
        }

        private void startCapturing()
        {

            leftSource.VideoCaptureDevice = leftDeviceComboBox.SelectedItem as VideoCaptureDevice;
            rightSource.VideoCaptureDevice = rightDeviceComboBox.SelectedItem as VideoCaptureDevice;

            if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
            {
                leftSource.Start();
                leftVideoBrush.SetSource(leftSource);

                if (leftSource.VideoCaptureDevice != rightSource.VideoCaptureDevice)
                {
                    rightSource.Start();
                    rightVideoBrush.SetSource(rightSource);
                }
                else
                {
                    MessageBox.Show("Only one innput device does not really make sense for this 3D application!", "Alert", MessageBoxButton.OK);
                    rightVideoBrush.SetSource(leftSource);
                }
            }

        }
    }
}

